package com.team.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;

import java.io.FileInputStream;

/**
 * oss文件操作的工具类
 */
public class OSSUtils {
    private final static  String endpoint="oss-cn-wuhan-lr.aliyuncs.com";  //服务器节点
    private final static String accessKeyId= "LTAI5tS5xTTNWa98tWpoj6QA";  //访问key
    private final static String accessKeySecret="T9NRsjbHx77fQYjbj2wNAkasZ5k0Kk";  //key对应的密钥
    private final static String bucketName="awei-bag"; //桶名

    /**
     * //编写上传文件的方法
     *  @param filename  上传的文件名称
     * @param fis  文件输入流
     * @return 上传文件后的访问路径
     */
    public static String upload_File(String filename,FileInputStream fis){
        try {
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            //2.使用OSSClient对象的方法操作对象
            //设置oss实例的访问权限：公共读
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            //ossClient.putObject("桶名","文件名称",文件对象);
            //获取源文件的输入流
            ossClient.putObject(bucketName, filename, fis);
            //3.关闭
            ossClient.shutdown();
            //4.获取图片的路径
            String path = "http://" + bucketName + "." + endpoint + "/" + filename;
            return path;
        }catch (Exception e){
            e.printStackTrace();
        }
       return null;
    }

}
